package com.nq.utils.task.stock;


import com.nq.service.IUserIndexPositionService;
import com.nq.service.IUserService;
import com.nq.service.IUserPendingorderService;
import com.nq.utils.DateTimeUtil;
import com.nq.utils.stock.BuyAndSellUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 强制平仓定时任务
 */
@Component
public class StockBusinessTask {

    private static final Logger log = LoggerFactory.getLogger(StockBusinessTask.class);


    @Autowired
    IUserService iUserService;
    @Autowired
    IUserPendingorderService userPendingorderService;
    @Autowired
    IUserIndexPositionService iUserIndexPositionService;


    /**
     * 挂单定时任务
     * 买入挂单
     */
//    @Scheduled(cron = "0/5 * * * * ?")
    public void buyHangSlips() {
        log.info("=====挂单定时任务【买入挂单】執行开始，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
        this.userPendingorderService.buyHangSlips();
        log.info("=====挂单定时任务【买入挂单】執行結束，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
    }

    /**
     * 挂单定时任务
     * 卖出
     */
//    @Scheduled(cron = "0/5 * * * * ?")
    public void sellOutHangSlips() {
        log.info("=====挂单定时任务【卖出挂单】執行开始，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
        this.userPendingorderService.orderSelloutTask();
        log.info("=====挂单定时任务【卖出挂单】執行結束，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
    }


    /**
     * 平仓用户持仓记录
     */
    @Scheduled(cron = "0 0/1 * * * MON-FRI")
    public void openPositionRecord() {

        log.info("=====掃描用戶持倉執行，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
        this.iUserService.ForceSellTask();
        log.info("=====掃描用戶持倉結束，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));

    }

    /**
     * 用戶持倉單-單支股票盈虧-強平定時
     */
//    @Scheduled(cron = "0 0/1 * * * MON-FRI")
    public void stockProfitLossOneTask() {
        log.info("=====掃描單支股票盈虧執行，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
        this.iUserService.ForceSellOneStockTask();
        log.info("=====掃描單支股票盈虧結束，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));
    }

    /*
    * 用戶股票持倉單-強平提醒推送消息定時
    * */
    @Scheduled(cron = "0 0/1 * * * *")
    public void banlanceUserPositionMessage() {

        boolean am = false;
        boolean pm = false;
        try {
            am = BuyAndSellUtils.isTransTime("9:30", "11:30");
            pm = BuyAndSellUtils.isTransTime("13:00", "15:00");
        } catch (Exception e) {
            log.error("執行定時任務出錯，e = {}", e);
        }


        log.info("當前 am = {}  pm = {}", Boolean.valueOf(am), Boolean.valueOf(pm));

        if (am || pm) {

            log.info("=====掃描用戶持倉執行，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));

            this.iUserService.ForceSellMessageTask();

            log.info("=====掃描用戶持倉結束，當前時間 {} =====", DateTimeUtil.dateToStr(new Date()));

        } else {

            log.info("當前時間不為周一至周五，或者不在交易時間內，不執行（強平）提醒推送消息任務");

        }

    }


}
